package com.lkm.app_ui.m.Imple.push;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RunTimeData;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.netapi.Api;



public class UpPushKeyTask extends TaskAdapter<Void, Void, Void> {


    public static String getTaskId() {
        return UpPushKeyTask.class.getName();
    }

    private String pushKey;

    public UpPushKeyTask(String pushKey) {
        super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), getTaskId(), AppConst.getTaskManager2());
        this.pushKey = pushKey;
//        Task task = TaskHelp.getTask(UpPushKeyTask.class.getName());




    }

    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);

        if (!getIsContinue()) return;

        mTaskExecutor.executorDoAsyncExecuting(new Runnable() {
            @Override
            public void run() {
                if (!getIsContinue()) return;

                if (RunTimeData.steelyardInfo == null || StringHelp.isEmpty(pushKey) || StringHelp.isEmpty(RunTimeData.steelyardInfo.getSteelyardMac()) || RunTimeData.steelyardInfo.getOrg() < 1) {

                    cancel();
                    return;
                }

                ResponEntity<Void> re = null;
                try {
                    re = ResponEntity.fromJson(
                            Api.upPushKey(RunTimeData.steelyardInfo.getSteelyardMac(), "" + RunTimeData.steelyardInfo.getOrg(), pushKey, AppConst.deviceUncode,UpPushKeyTask.this)
                            , null, "");
                } catch (Exception e) {
                    
                    re = new ResponEntity().fail("");
                }

                if (re.isSuccess()) {
                    complete(null);
                } else {
                    if (!getIsContinue()) return;
                    mTaskExecutor.executeOnDispatchDelayed(new Runnable() {
                        @Override
                        public void run() {
                            doExecuting(getParam());
                        }
                    }, 2000);

                }

            }
        });

    }
}
